using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Material.DAL
{
    public static class Excutor
    {
        public static int ExecuteNonQuery(Accessor ctx)
        {
           
            int result = -1;
            
            try
            {
                result = ctx.Command.ExecuteNonQuery();
               
            }
            catch (Exception ex)
            {
                ctx.WriteMessage(ex.Message);
              
            }
            finally
            {
                ctx.Close();
            }
            return result;
        }

        public static DataSet GetDataSet(Accessor ctx)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(ctx.Command);// ctx.DataAdapter;
             
           
            try
            {
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                ctx.WriteMessage(ex.Message);
            }
            finally
            {
                ctx.Close();
            }
            return ds;
        }

        public static SqlDataReader GetDataReader(Accessor ctx)
        {
            SqlDataReader idr = null;
            try
            {
                idr = ctx.Command.ExecuteReader();
            }
            catch (Exception ex)
            {
                ctx.WriteMessage(ex.Message);
            }
            finally
            {
                ctx.Clear();
            }
            return idr;
        }

        public static object ExecuteScalar(Accessor ctx)
        {
            object result = -1;
            try
            {
                result = ctx.Command.ExecuteScalar();
            }
            catch (Exception ex)
            {
                ctx.WriteMessage(ex.Message);
            }
            finally
            {
                ctx.Close();
            }
            return result;
        }
    }
}
